<?php

require_once ('system/libraries/Model.php');
require_once 'system/application/models/interfaces.php';
class NewsModel extends Model implements Updatable  {
	
	private $newsID = 0;
	private $titleMsgID = 0;
	private $title = null;
	private $textMsgID = 0;
	private $text = null;
	private $userID = 0;
	private $user = null;
	private $langID = 0;
	private $date = null;
	
	function NewsModel($id = null) {
		parent::__construct ();
		if ($id != null) {
			try {
				// if we have the $id, let's get the data
				$queryString = "select n.*, m.content as title, m1.content as text 
									from news n,messages m, messages m1 
									where n.newsID=? and n.titleMsgID=m.messageID and 
											n.textMsgID=m1.messageID";
				$query = $this->db->query($queryString, $id);
				foreach ($query->result() as $row) {
					$this->setDate($row->date);
					$this->setLangID($row->langID);
					$this->setNewsID($row->newsID);
					$this->setText($row->text);
					$this->setTextMsgID($row->textMsgID);
					$this->setTitle($row->title);
					$this->setTitleMsgID($row->titleMsgID);
					$this->setUserID($row->userID);
				}
				if ($this->getNewsID() == 0) {
					throw new Exception("No news with id $id");
				}
			} catch (Exception $e) {
				// we will get here if we get the error from the UserModel constructor
				// or from the NewsModel constructor
				echo $e->getMessage() . "<br />";
				echo $e->getTraceAsString();
			}
		}
	}
	
	public function getNewsID() {
		return $this->newsID;
	}
	
	public function getTitle() {
		return $this->title;
	}
	
	public function getTitleMsgID() {
		return $this->titleMsgID;
	}
	
	public function getText() {
		return $this->text;
	}
	
	public function getTextMsgID() {
		return $this->textMsgID;
	}
	
	public function getUserID() {
		return $this->userID;
	}
	
	public function getUser() {
		return $this->user;
	}
	
	public function getLangID() {
		return $this->langID;
	}
	
	public function getDate() {
		return $this->date;
	}
	public function setNewsID($newsID) {
		$this->newsID = $newsID;
	}
	
	public function setTitleMsgID($titleMsgID) {
		$this->titleMsgID = $titleMsgID;
	}
	
	public function setTitle($title) {
		$this->title = $title;
	}
	
	public function setTextMsgID($textMsgID) {
		$this->textMsgID = $textMsgID;
	}
	
	public function setText($text) {
		$this->text = $text;
	}
	
	public function setUserID($userID) {
		$this->userID = $userID;
		$this->user = new UserModel($userID);
	}
	
	public function setUser($user) {
		$this->user = $user;
		if ($user != null) {
			$this->userID = $user->getUserID();
		} else {
			$this->userID = 0;
		}
	}

	
	public function setLangID($langID) {
		$this->langID = $langID;
	}
	
	public function setDate($date) {
		$this->date = $date;
	}
	function __toString() {
    	return $this->getTitle()." - lang: ". $this->getLangID() . "<br />";
	}
	
	public function updateDatabase(){
		// title
		$queryString = "update messages set content=?, langID=? where messageID=?";
		$this->db->query($queryString, array($this->getTitle(), $this->getLangID(), $this->getTitleMsgID()));
		
		// text
		$queryString = "update messages set content=?, langID=? where messageID=?";
		$this->db->query($queryString, array($this->getText(), $this->getLangID(), $this->getTextMsgID()));
		
		// news lang
		$queryString = "update news set langID=? where newsID=?";
		$this->db->query($queryString, array($this->getLangID(), $this->getNewsID()));

	}
	public function restoreFromDatabase(){
		
	}
	public function deleteFromDatabase(){
		$queryString = "delete from messages where messageID=? or messageID=?";
		$query = $this->db->query($queryString, array($this->getTextMsgID(), $this->getTitleMsgID()));
		if (count($query) > 0) {
			$queryString = "delete from news where newsID=?";
			$query = $this->db->query($queryString, array($this->getNewsID()));
			if (count($query) == 0) {
				throw new Exception("Messages deleted, news not detelted! major error, no rollback!");
			}
		} else {
			throw new Exception("News with id ". $this->getNewsID(). 
				" has NOT been delete from the database");
		}
	}

}

?>
